<!--  -->
<template>
  <div class="">
    <el-card>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">用户管理</a></el-breadcrumb-item>
        <el-breadcrumb-item>用户等级</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row>
        <el-col :span="24">
          <el-button type="primary" @click="addUserLevelFn"
            >添加用户等级</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column prop="id" label="序号" width="180"> </el-table-column>
        <el-table-column prop="title" label="名称" width="180">
        </el-table-column>
        <el-table-column prop="img" label="头像" width="80">
          <template slot-scope="scope">
            <el-avatar size="medium" :src="scope.row.img"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="is_show" label="是否可用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="开"
              inactive-text="关"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              @click="handleEditLevelClick(scope.row)"
              type="primary"
              size="small"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="deleUserLevelFn(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加用户等级弹窗 -->
      <el-dialog title="添加用户等级" :visible.sync="dialogFormVisible">
        <el-form :model="addLevelDatas" label-width="80px">
          <el-form-item label="名称">
            <el-input
              v-model="addLevelDatas.title"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="是否可用">
            <el-switch
              v-model="addLevelDatas.is_show"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="可用"
              inactive-text="禁用"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- 修改用户等级弹窗 -->
      <el-dialog title="修改用户等级" :visible.sync="dialogEditFormVisible">
        <el-form :model="editLevelDatas" label-width="80px">
          <el-form-item label="名称">
            <el-input
              v-model="editLevelDatas.title"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="是否可用">
            <el-switch
              v-model="editLevelDatas.is_show"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="可用"
              inactive-text="禁用"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEditFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogEditFormVisible = false"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  //ComponentName和文件名保持一致
  name: "ComponentName",
  data() {
    //这里存放数据
    return {
      dialogFormVisible: false,
      dialogEditFormVisible: false,
      imageUrl: "",
      addLevelDatas: {
        title: "",
        is_show: "1",
      },
      editLevelDatas: {
        title: "",
        is_show: "1",
      },
      tableData: [
        {
          id: 1,
          title: "管理员",
          img: require("@/assets/images/01.jpg"),
          is_show: "1",
        },
        {
          id: 2,
          title: "金牌用户",
          img: require("@/assets/images/01.jpg"),
          is_show: "1",
        },
        {
          id: 3,
          title: "银牌用户",
          img: require("@/assets/images/01.jpg"),
          is_show: "1",
        },
        {
          id: 4,
          title: "铜牌用户",
          img: require("@/assets/images/01.jpg"),
          is_show: "1",
        },
      ],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    handleEditLevelClick(row) {
      console.log(row);
      this.editLevelDatas = row;
      this.dialogEditFormVisible = true;
    },
    addUserLevelFn() {
      this.dialogFormVisible = true;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    deleUserLevelFn(row) {
      console.log(row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "残忍删除",
        cancelButtonText: "再想想",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  deactivated() {}, //如果页面有keep-alive缓存功能，当组件被移除时，触发钩子函数deactivated
};
</script>
<style>
</style>